package com.example.smartcity_poverty.ui.AllService.StopWhere

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.Base.BaseRecyclerAdapter
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.model.stop.StopWhereTime
import com.example.smartcity_poverty.logic.util.Util.show
import kotlinx.android.synthetic.main.activity_stop_sw_all.*
import kotlinx.android.synthetic.main.re_item_stopall.*

/**
 * 停车记录查询
 */
class StopSwAllActivity : AppCompatActivity() {

    private val viewModel by lazy {
        ViewModelProviders.of(this)[StopWhereViewModel::class.java]
    }

    // 默认显示数据数量
    private var size = 6

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_stop_sw_all)
        title = "停车记录"

        inputOk.setOnClickListener {
            if (inputEditText.text.toString().isEmpty()){
                "输入不能为空".show()
            }else{
                // 查找停车记录列表
                setStop(inputEditText.text.toString())
            }
        }

        // 点击更新数据
        stopAdd.setOnClickListener {
            size+=6
            StopWhereAllRv.adapter?.notifyItemChanged(viewModel.stopSwList.size)
            setStop(inputEditText.text.toString())
        }
    }

    /**
     * 查询停车记录列表
     */
    private fun setStop(input: String) {
        viewModel.stopSwAll(input)
        viewModel.stopSwLiveData.observe(this, Observer { resources ->
            val body = resources.getOrNull()
            body?.let {
                viewModel.stopSwList.clear()
                viewModel.stopSwList.addAll(body.rows!!)
                if (viewModel.stopSwList.isEmpty()){

                }else{
                    StopLayout.visibility = View.VISIBLE
                    // 填充数据给适配器
                    StopWhereAllRv.apply {
                        this.layoutManager = LinearLayoutManager(context)
                        this.adapter = StopAdapter(viewModel.stopSwList, R.layout.re_item_stopall, size)
                        this.isNestedScrollingEnabled = false
                    }
                    SizeNumber.text = "为你找到${size}条停车记录"
                    "为你加载了${size}条停车记录".show()
                }
            }
        })
    }

    /**
     * 列表适配器
     */
    inner class StopAdapter(
        val list: MutableList<StopWhereTime.RowsBean>,
        val layoutId: Int,
        val size: Int
    ) : BaseRecyclerAdapter<StopWhereTime.RowsBean>(list, layoutId){
        override fun setData(
            view: BaseViewHolder.ViewFind,
            data: StopWhereTime.RowsBean,
            position: Int,
            holder: BaseViewHolder
        ) {
            val d = list[position]
            val v = holder.viewFind
            val con = v.view?.context

            // 填充数据
            v.sInputTime.text = "入场时间: ${d.entryTime}"
            v.sOutTime.text = "出场时间: ${d.outTime}"
            v.sThis.text = "地址: ${d.parkName}"
            v.sNumber.text = "车牌号: ${d.plateNumber}"
            v.sDao.text = "收费: ${d.monetary}$"
        }

        override fun getItemCount(): Int = size
    }




    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}